package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.wemedia.receive.dtos.*;
import com.heima.wemedia.receive.page.PageResponseResult;
import com.heima.wemedia.receive.vo.CommentVo;
import com.heima.wemedia.service.WmCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自媒体-评论管理
 *
 * @author zex
 * @date 2024/08/01
 */
@RestController
@RequestMapping("/api/v1/comment/manage")
@Slf4j
@Api(tags = "自媒体-评论管理")
public class WmCommentController {

    @Autowired
    private WmCommentService wmCommentService;

    /**
     * 评论管理查询
     *
     * @param commentPageDto
     * @return
     */
    @PostMapping("/find_news_comments")
    public PageResponseResult<List<CommentVo>> fin(@RequestBody CommentPageDto commentPageDto) {
        Page<CommentVo> wmNewsPage = wmCommentService.findNewsComments(commentPageDto);
        return PageResponseResult.okResult(wmNewsPage.getCurrent(), wmNewsPage.getSize(), wmNewsPage.getTotal(), wmNewsPage.getRecords());
    }

    /**
     * 作者回复评论
     *
     * @param commentRepayDto commentId 评论id content 评论内容
     * @return
     */
    @PostMapping("/comment_repay")
    @ApiOperation(value = "作者回复评论")
    public ResponseResult replyComment(@RequestBody CommentRepayDto commentRepayDto) {
        log.info("作者回复评论{}", commentRepayDto);
        return wmCommentService.replyComment(commentRepayDto);
    }

    /**
     * 查看回复评论列表
     *
     * @param cPInfoDto articleId文章id
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "查看回复评论列表")
    public ResponseResult findNewsComments(@RequestBody CommentPageInfoDto cPInfoDto) {
        log.info("查看回复评论列表{}", cPInfoDto);
        return wmCommentService.findReplyComments(cPInfoDto);
    }

    /**
     * 删除评论
     *
     * @param commentId
     * @return
     */
    @DeleteMapping("/del_comment/{commentId}")
    @ApiOperation(value = "删除评论")
    public ResponseResult delComment(@PathVariable String commentId) {
        log.info("根据评论id删除评论{}", commentId);
        return wmCommentService.delComment(commentId);
    }

    /**
     * 删除评论回复
     *
     * @param commentRepayId
     * @return
     */
    @DeleteMapping("/del_comment_repay/{commentRepayId}")
    @ApiOperation(value = "删除评论回复")
    public ResponseResult delCommentRepay(@PathVariable String commentRepayId) {
        log.info("根据回复评论id删除{}", commentRepayId);
        return wmCommentService.delCommentRepay(commentRepayId);
    }


    /**
     * 打开或关闭评论
     *
     * @param updateIsCommentDto
     * @return {@link ResponseResult }
     */
    @PostMapping("/update_comment_status")
    public ResponseResult updateCommentStatus(@RequestBody UpdateIsCommentDto updateIsCommentDto) {
        log.info("打开或关闭评论:{}", updateIsCommentDto.getOperation() == 0 ? "关闭评论" : "打开评论");
        wmCommentService.updateCommentStatus(updateIsCommentDto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /**
     * 评论点赞
     *
     * @param LikesDto
     * @return {@link ResponseResult }<{@link Map }<{@link String }, {@link Integer }>>
     */
    @PostMapping("/like")
    public ResponseResult<HashMap<String, Object>> updateCommentLikes(@RequestBody CommentLikeDto LikesDto) {
        log.info("评论点赞参数:{}", LikesDto);
        Long likes = wmCommentService.updateCommentLike(LikesDto);
        HashMap<String, Object> map = new HashMap<>();
        map.put("likes", likes);
        return ResponseResult.okResult(map);
    }


}
